#include<iostream>
#include<unordered_map>
using namespace std;
unordered_map<int, int>mp;
const int N = 2e5 + 10;
int p[N];
int n, m;
struct S
{
	int a, b, e;
}s[N];
int get(int x)
{
	if (mp.count(x) == 0) {
		mp[x] = ++n;
	}
	return mp[x];
}
int find(int x)
{
	if (x != p[x]) {
		p[x] = find(p[x]);
	}
	return p[x];
}
int main()
{
	int t;
	cin >> t;
	while (t--) {
		mp.clear();
		cin >> m;
		for (int i = 0; i < m; i++) {
			int a, b, c;
			cin >> a >> b >> c;
			s[i] = { get(a),get(b),c };
		}
		for (int i = 1; i <= n; i++) {
			p[i] = i;
		}
		for (int i = 0; i < m; i++) {
			if (s[i].e == 1) {
				int a = find(s[i].a);
				int b = find(s[i].b);
				if (a != b) {
					p[a] = b;
				}
			}
		}
		bool flag = 0;
		for (int i = 0; i < m; i++) {
			if (!s[i].e) {
				int a = find(s[i].a);
				int b = find(s[i].b);
				if (a == b) {
					flag = 1;
					break;
				}
			}
		}
		if (flag) cout << "NO" << endl;
		else cout << "YES" << endl;
	}
	return 0;
}